//
//  GRMyPaidOrderViewController.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/8/30.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRMyPaidOrderViewController.h"
#import "GRMyPaidOrderViewCell.h"
#import "GRPayOrderDatailVC.h"
@interface GRMyPaidOrderViewController ()

@property (nonatomic, strong) NSMutableArray *billList;

@end

@implementation GRMyPaidOrderViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isRoot) {
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.isRoot) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.commontitle = @"我的买单";
    
    if (self.isRoot) {
        CGFloat height = [UIApplication sharedApplication].statusBarFrame.size.height + 44 + 12;
        self.commonHeader.frame = CGRectMake(0, 0, KscreenWidth, height);
        self.titleLabel.frame        = CGRectMake(16, height - 42, KscreenWidth-32, 30);
        self.tableView.frame        = CGRectMake(0, height, KscreenWidth, KscreenHeight - height);
    }
    
    self.blankSpace.contentMode = GRBlankContentModeEmptyPaidOrder;
    // Do any additional setup after loading the view.
    self.tableView = [self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
    [self.tableView registerNib:[UINib nibWithNibName:@"GRMyPaidOrderViewCell" bundle:nil] forCellReuseIdentifier:@"GRMyPaidOrderViewCell"];
    self.tableView.rowHeight = 115;
    
    [self getMyBill];
    [self MJRefresh];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doLoginOrLogout:) name:LoginToExitNotificationName object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doLoginOrLogout:) name:@"freshList" object:nil];
}

- (void)doLoginOrLogout:(NSNotification *)notification {
    
        [self.tableView.mj_header beginRefreshing];
 
}

static NSInteger pageIndex = 1;
- (void)getMyBill {
    if (![GRUserBasisInfo isLogin]) {
        [self.billList removeAllObjects];
        self.blankSpace.hidden = self.billList.count == 0 ? NO : YES;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        return;
    }
    NSDictionary *paramer = @{@"currentPage" : @(pageIndex),
                                          @"pageSize"      : @10
                                          };
    WEAKSELF
    [GRUserAction getMyBillListWithParamer:paramer completionHandler:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            if (pageIndex == 1) {
                [weakSelf.billList removeAllObjects];
            }
            [weakSelf.billList addObjectsFromArray:responseObject];
            weakSelf.blankSpace.hidden = weakSelf.billList.count == 0 ? NO : YES;
            [weakSelf.tableView reloadData];
        }else {
            [error errorAlertWithUIView:weakSelf.view offsetY:0 completion:^{
                [weakSelf getMyBill];
            }];
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (void)MJRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pageIndex = 1;
        [self getMyBill];
    }];
    
    self.tableView.mj_footer = [GRRefreshBackFooter footerWithRefreshingBlock:^{
        pageIndex += 1;
        [self getMyBill];
    }];
}


#pragma MARK ----------  <UITableViewDelegate /  UITableViewDataSource>  -------------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.billList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRMyPaidOrderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GRMyPaidOrderViewCell" forIndexPath:indexPath];
    cell.billModel                       = self.billList[indexPath.section];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GRPayOrderDatailVC * payOrderDetailVC = [[GRPayOrderDatailVC  alloc]init];
    GRMyBillModel *billmodel = self.billList[indexPath.section];
    payOrderDetailVC.orderId = billmodel.id;
    [self.navigationController pushViewController:payOrderDetailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)billList {
    if (!_billList) {
        _billList = [NSMutableArray array];
    }
    return _billList;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
